package bravo.xc.com.xc_bravo_android.operation.login;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.base.BaseMVPActivity;
import bravo.xc.com.xc_bravo_android.beans.AliaBeans;
import bravo.xc.com.xc_bravo_android.beans.LoginBeans;
import bravo.xc.com.xc_bravo_android.beans.LoginResultBeans;
import bravo.xc.com.xc_bravo_android.operation.person_center.PersonalCenterActivity;
import bravo.xc.com.xc_bravo_android.operation.re_password.RePasswordActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;
import rx.Observable;

import static bravo.xc.com.xc_bravo_android.beans.AliaBeans.LOGINOUT;

public class LoginActivity extends BaseMVPActivity<LoginContract.LoginView,LoginContract.LoginPresenter> implements LoginContract.LoginView,View.OnClickListener{

    private TextView tv_forgetPassword;//忘记密码
    private Button btn_login;//登录按钮
    private ProgressDialog progressDialog;
    private EditText et_account;//输入的帐号
    private EditText et_password;//输入的密码
    public static LoginBeans loginBeans;
    public static LoginContract.LoginPresenter loginPresenter;
    private SharedPreferences isLogin,LoginInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn_login:
                if(checkNull()){
                    loginBeans.setUsername(et_account.getText().toString().trim());
                    loginBeans.setPassword(et_password.getText().toString().trim());
                    SharedPreferences username = getSharedPreferences("username",MODE_PRIVATE);
                    SharedPreferences.Editor editor = username.edit().clear();
                    editor.putString("username",et_account.getText().toString().trim());
                    editor.commit();
                    loginPresenter = new LoginPresenter();
                    loginPresenter = presenter;
                    setAlia();
                    showProgress(checkNull());
                    /*loginBeans.setUsername(et_account.getText().toString().trim());
                    loginBeans.setPassword(et_password.getText().toString().trim());
                    presenter.doLogin(loginBeans);*/
                }
                break;
            case R.id.tv_forgetPassword:
                Intent intent=new Intent(LoginActivity.this,RePasswordActivity.class);
                startActivity(intent);
                break;
        }
    }


    @Override
    protected LoginContract.LoginPresenter createPresnter() {
        return new LoginPresenter();
    }

    /**
     * Input the number you want to set null
     * 0-null
     * 1-password
     * 2-username and password
     *
     * @param choice
     */
    @Override
    public void setNull(int choice) {
        switch (choice){
            case 0:
                break;
            case 1:
                et_password.setText("");
                break;
            case 2:
                et_password.setText("");
                et_account.setText("");
                break;
        }
    }

    @Override
    public void showProgress(boolean show) {

        if(progressDialog.isShowing()){
            progressDialog.dismiss();
            Log.e("@@showProgress","isShowing:dismiss");
        }
        if(show){
            progressDialog.show();
        }else {
            progressDialog.dismiss();
            Log.e("@@showProgress","dismiss");
        }

    }

    @Override
    protected void initView() {

        btn_login = (Button)findViewById(R.id.btn_login);
        et_account = (EditText)findViewById(R.id.et_account);
        et_password = (EditText)findViewById(R.id.et_password);
        tv_forgetPassword = (TextView)findViewById(R.id.tv_forgetPassword);

        progressDialog = new ProgressDialog(this,R.style.MyDialogStyle);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCanceledOnTouchOutside(false);

        btn_login.setOnClickListener(this);
        tv_forgetPassword.setOnClickListener(this);

    }


    public void removeUserInfo() {
        getSharedPreferences("isLogin",MODE_PRIVATE).edit().putBoolean("flag",false).commit();
        getSharedPreferences("UserInfo",MODE_PRIVATE).edit().clear();
    }

    @Override
    protected void initData() {
        context = this;
        loginBeans = new LoginBeans();
        removeUserInfo();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected Unbinder getBinder() {
        return ButterKnife.bind(this);
    }

    @Override
    protected void onChildDestroy() {
        presenter.onDetached();
    }

    @Override
    public boolean checkNull() {
        if (et_account.getText().toString().trim().isEmpty()) {
            showToast("请填写用户名");
            return false;
        }
        if (et_password.getText().toString().trim().isEmpty()) {
            showToast("请填写密码");
            return false;
        }
        return true;
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(LoginActivity.this,str,Toast.LENGTH_SHORT).show();
    }

    /**
     * Input the activity you want to go to
     * 0-rePassword
     * 1-PersonalCenter
     * @param intent
     */
    @Override
    public void toNewActivity(int intent){


        Intent toNewActivity;

        if(intent == 1){
            toNewActivity = new Intent(LoginActivity.this, PersonalCenterActivity.class);
        }else{
            toNewActivity = new Intent(LoginActivity.this, RePasswordActivity.class);
        }
        startActivity(toNewActivity);
        this.finish();
    }

    @Override
    public void setSP(LoginResultBeans loginResultBeans) {
        isLogin = getSharedPreferences("isLogin",MODE_PRIVATE);
        SharedPreferences.Editor editor = isLogin.edit();
        editor.putBoolean("flag",true);
        editor.putString("username",et_account.getText().toString().trim());
        editor.commit();

        LoginInfo = getSharedPreferences("UserInfo",MODE_PRIVATE);
        SharedPreferences.Editor editorInfo = LoginInfo.edit();
        editorInfo.putString("username",et_account.getText().toString().trim());
        editorInfo.putString("shopname",loginResultBeans.getData().get(0).getShopname());
        editorInfo.putInt("userId",loginResultBeans.getData().get(0).getUserId());
        editorInfo.commit();
    }

    @Override
    public void setAlia(){
        JPushInterface.setAlias(LoginActivity.this, AliaBeans.LOGIN,getSharedPreferences("username",MODE_PRIVATE).getString("username",null));
    }

}
